package com.ssyc.common.manager;

import android.util.Log;

import com.netease.nimlib.sdk.NIMClient;
import com.netease.nimlib.sdk.Observer;
import com.netease.nimlib.sdk.StatusCode;
import com.netease.nimlib.sdk.auth.AuthServiceObserver;
import com.netease.nimlib.sdk.auth.ClientType;
import com.netease.nimlib.sdk.auth.OnlineClient;
import com.ssyc.common.base.BaseActivity;
import com.ssyc.common.bean.BusInfo;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

/**
 * IM互登下线工具类
 * Created by Kevin on 2018/8/9.
 */

public class IMUtils {


    /**
     * 登录成功后注册
     *
     * @param register //
     */
    public static void registerObservers(boolean register) {
//        NIMClient.getService(AuthServiceObserver.class).observeOtherClients(clientsObserver, register);
        NIMClient.getService(AuthServiceObserver.class).observeOnlineStatus(userStatusObserver, register);
    }


    public static Observer<List<OnlineClient>> clientsObserver = new Observer<List<OnlineClient>>() {
        @Override
        public void onEvent(List<OnlineClient> onlineClients) {

            Log.i("test", "其他段登录了" + onlineClients);

            if (onlineClients == null || onlineClients.size() == 0) {
                return;
            } else {
                OnlineClient client = onlineClients.get(0);
                switch (client.getClientType()) {
                    case ClientType.Windows:
                    case ClientType.MAC:
                        break;
                    case ClientType.Web:
                        break;
                    case ClientType.iOS:
                    case ClientType.Android:
                        break;
                    default:
                        break;
                }
            }
        }
    };

    /**
     * 用户状态变化
     */
    public static Observer<StatusCode> userStatusObserver = new Observer<StatusCode>() {

        @Override
        public void onEvent(StatusCode code) {

            if (code == StatusCode.KICKOUT) {
                //被踢下线了,弹框提示跳转到角色选择界面
                BusInfo info = new BusInfo();
                info.setType(BaseActivity.KICKOUT);
                EventBus.getDefault().post(info);
            }
        }
    };
}
